// Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.

// fleet创建结构体定义
package fleet

import (
	"fleetmanager/setting"
)

/*
1. Pod中所有容器的CPU总和须为0.25的倍数
2. Pod中所有容器的内存总和须为1的倍数
3. Pod的内存/CPU比值区间为[2, 8]
*/
type PodSpecification struct {
	CPU    int `json:"cpu"`
	Memory int `json:"memory"`
}

type ResourceSpecificationRequest struct {
	InstanceType          string   `json:"instance_type" validate:"oneof=VM POD"`
	Name                  string   `json:"name" validate:"min=1,max=1024"`
	Region                string   `json:"region" validate:"min=1,max=64"`
	ResourceSpecification string   `json:"resource_specification" validate:"min=1,max=1024"`
	Flavor                []string `json:"flavor" validate:"min=1,max=10,dive,min=1,max=1024"`
}

type CreateRequest struct {
	InstanceType                            string                      `json:"instance_type" validate:"oneof=VM POD"`
	Name                                    string                      `json:"name" validate:"min=1,max=1024"`
	Description                             string                      `json:"description" validate:"omitempty,min=1,max=1024"`
	BuildId                                 string                      `json:"build_id" validate:"min=1,max=128"`
	Region                                  string                      `json:"region" validate:"min=1,max=64"`
	PublicIp                                PublicIp                    `json:"public_ip" validate:"dive"`
	ServerSessionProtectionPolicy           string                      `json:"server_session_protection_policy" validate:"oneof=NO_PROTECTION FULL_PROTECTION TIME_LIMIT_PROTECTION"`
	ServerSessionProtectionTimeLimitMinutes int                         `json:"server_session_protection_time_limit_minutes" validate:"gte=5,lte=1440"`
	RuntimeConfiguration                    RuntimeConfiguration        `json:"runtime_configuration" validate:"required,dive"`
	InstanceTags                            []InstanceTag               `json:"instance_tags" validate:"omitempty,min=0,max=10,scalingTagsNotDelicated,dive"`
	ResourceCreationLimitPolicy             ResourceCreationLimitPolicy `json:"resource_creation_limit_policy"`
	EnterpriseProjectId                     string                      `json:"enterprise_project_id" validate:"omitempty,max=64"`
	VPCId                                   string                      `json:"vpc_id" validate:"omitempty,max=64"`
	SubnetId                                string                      `json:"subnet_id" validate:"omitempty,max=64"`
	InboundPermissions                      []IpPermission              `json:"inbound_permissions" validate:"required,min=1,dive"`
	SecurityGroupIds                        []string                    `json:"security_group_ids" validate:"min=0,max=10,dive,min=0,max=128"`
	ScalingGroupCapacity                    ScalingGroupCapacity        `json:"scaling_group_capacity" validate:"dive"`
	ResourceSpecificationId                 string                      `json:"resource_specification_id" validate:"min=1,max=128"`
	AvailableZones                          []string                    `json:"available_zones" validate:"omitempty,min=0,max=10,dive,min=1,max=64"`
	PodSpecification                        PodSpecification
}

//SubnetId is the networkID of the subnet, and IPv4SubnetId is the ipv4 subnet ID of the subnet
type FleetVpcAndSubnet struct {
	VpcId        string
	VpcName      string
	VpcCidr      string
	SubnetId     string
	SubnetName   string
	SubnetCidr   string
	GatewayIp    string
	IPv4SubnetId string
}

type ScalingGroupCapacity struct {
	MinInstanceNumber int32 `json:"min_instance_number" validate:"gte=1,ltefield=MaxInstanceNumber"`
	MaxInstanceNumber int32 `json:"max_instance_number" validate:"gte=1,lte=20000"`
}

type PublicIp struct {
	IpType    string    `json:"ip_type" validate:"required,min=1,max=128"`
	BandWidth Bandwidth `json:"bandwidth" validate:"required,dive"`
}

type Bandwidth struct {
	ShareType    string `json:"share_type" validate:"oneof=WHOLE PER"`
	Size         int    `json:"size" validate:"gte=1,lte=2000"`
	ChargingMode string `json:"charging_mode" validate:"oneof=bandwidth traffic"`
	Id           string `json:"id" validate:"min=0,max=64"`
}

type CreateResponse struct {
	Fleet Fleet `json:"fleet"`
}

type ResourceSpecificationResponse struct {
	InstanceType          string               `json:"instance_type" validate:"oneof=VM POD"`
	Id                    string               `json:"id" validate:"min=0,max=64"`
	Region                string               `json:"region" validate:"min=1,max=64"`
	Name                  string               `json:"name" validate:"min=1,max=1024"`
	ResourceSpecification string               `json:"resource_specification" `
	Flavor                []string             `json:"flavor" `
	CreationTime          string               `json:"creation_time"`
	UpdateTime            string               `json:"update_time"`
	FleetCount            int64                `json:"fleet_count"`
	AssociatedFleets      []AssociatedFleetRsp `json:"associated_fleets"`
}

// AssociatedFleetRs: 实例规格关联fleet响应
type AssociatedFleetRsp struct {
	Name                string `json:"name"`
	FleetId             string `json:"fleet_id"`
	State               string `json:"state"`
	InstanceCount       int    `json:"instance_count"`
	ProcessCount        int    `json:"process_count"`
	MaxServerSessionNum int    `json:"max_server_session_count"`
	CreationTime        string `json:"creation_time"`
}

// NewCreateRequest: 新建创建fleet请求体
func NewCreateRequest() *CreateRequest {
	r := &CreateRequest{
		Region: setting.DefaultFleetRegion,
		PublicIp: PublicIp{
			IpType: setting.DefaultEipType,
			BandWidth: Bandwidth{
				ShareType:    setting.DefaultEipShareType,
				Size:         setting.DefaultFleetBandwidth,
				ChargingMode: setting.DefaultFleetBandwidthChargingMode,
				Id:           "",
			},
		},
		ServerSessionProtectionPolicy:           setting.DefaultFleetProtectPolicy,
		ServerSessionProtectionTimeLimitMinutes: setting.DefaultFleetProtectTimeLimit,
		RuntimeConfiguration: RuntimeConfiguration{
			ServerSessionActivationTimeoutSeconds: setting.DefaultFleetSessionTimeoutSeconds,
			MaxConcurrentServerSessionsPerProcess: setting.DefaultFleetMaxSessionNumPerProcess,
		},
		ResourceCreationLimitPolicy: ResourceCreationLimitPolicy{
			PolicyPeriodInMinutes: setting.DefaultFleetPolicyPeriod,
			NewSessionsPerCreator: setting.DefaultFleetNewSessionNumPerCreator,
		},
		EnterpriseProjectId: setting.DefaultEnterpriseProject,
		VPCId:               "",
		SubnetId:            "",
		SecurityGroupIds:    nil,
		ScalingGroupCapacity: ScalingGroupCapacity{
			MinInstanceNumber: setting.DefaultGroupMinSizeValue,
			MaxInstanceNumber: setting.DefaultGroupMaxSizeValue,
		},
	}
	return r
}

// NewResourceSpecificationRequest: 新建创建实例规格请求体
func NewResourceSpecificationRequest() *ResourceSpecificationRequest {
	re := &ResourceSpecificationRequest{
		Name:                  "",
		ResourceSpecification: "",
	}
	return re
}
